package com.cskaoyan.jdbc.datasource;

import com.cskaoyan.jdbc.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 创建日期: 2022/05/13 17:29
 *
 * @author ciggar
 */
public class DatasourceDemo {

    public static void main(String[] args) throws SQLException {

        // 获取连接
        Connection connection1 = MyConnectionPool3.getConnection();
        Connection connection2 = MyConnectionPool3.getConnection();
        Connection connection3 = MyConnectionPool3.getConnection();
        Connection connection4 = MyConnectionPool3.getConnection();
        Connection connection5 = MyConnectionPool3.getConnection();
        Connection connection6 = MyConnectionPool3.getConnection();
        Connection connection7 = MyConnectionPool3.getConnection();


        Statement statement = connection6.createStatement();

        ResultSet resultSet = statement.executeQuery("select * from account");

        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("id:" + id + ", name:" +name);
        }

        // 关闭资源
        JDBCUtils.close(connection1,statement,resultSet);

        // 返回连接到连接池中
        MyConnectionPool3.returnConnection(connection1);
        MyConnectionPool3.returnConnection(connection2);
        MyConnectionPool3.returnConnection(connection3);
        MyConnectionPool3.returnConnection(connection4);
        MyConnectionPool3.returnConnection(connection5);
        MyConnectionPool3.returnConnection(connection6);
        MyConnectionPool3.returnConnection(connection7);

    }
}
